package com.ysynzehao.controller;

import com.ysynzehao.bean.user.User;
import com.ysynzehao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * niuzehao
 * 2018/11/30 10:55.
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("list")
    public ResponseEntity<Page<User>> list(@PageableDefault(page = 0, size = 10, sort = "userId", direction = Sort.Direction.ASC) Pageable pageable) {
        Page<User> allUser = userService.getAllUser(pageable);
        return ResponseEntity.ok(allUser);
    }

    @GetMapping("list2")
    public ResponseEntity<Page<User>> list2(@PageableDefault(page = 0, size = 10, sort = "userId", direction = Sort.Direction.ASC) Pageable pageable, User user) {
        Page<User> allUser = userService.getAllUser2(pageable, user);
        return ResponseEntity.ok(allUser);
    }

    @GetMapping("listUserMap")
    public ResponseEntity<List<Map>> listUserMap() {
        List<Map> allUser = userService.getUserMap();
        return ResponseEntity.ok(allUser);
    }

    @GetMapping("getUser/{userId}")
    public ResponseEntity<User> getUser(@PathVariable("userId") Integer userId) {
        User user = userService.getUser(userId);
        return ResponseEntity.ok(user);
    }

    @PostMapping("saveUser")
    public ResponseEntity<User> saveUser(@RequestBody User user) {
        user = userService.saveUser(user);
        return ResponseEntity.ok(user);
    }

    @PutMapping("updateUser")
    public ResponseEntity<User> updateUser(@RequestBody User user) {
        user = userService.saveUser(user);
        return ResponseEntity.ok(user);
    }

    @DeleteMapping("deleteUser/{userId}")
    public ResponseEntity<String> deleteUser(@PathVariable("userId") int userId) {
        userService.deleteUser(userId);
        return ResponseEntity.ok("ok");
    }
}
